<!-- 首页的即时秒杀 -->
<template>
	<view class="page">
		<view class="head-back">
			<view class="back" @click="onBack">
				<text></text>
			</view>
			<view class="title">
				<image src="/static/flash_title.png" mode=""></image>
			</view>
		</view>
		<view class="head-bg">
			<view class="session" :class="{'activity':scrollTop > 50}">
				<view style="margin-left: 27%;margin-top: 10px;">
					<text style="color: #ffffff; font-size: 35rpx;">每天下午四点准时开抢！</text>
				</view>
			</view>
			<view style="margin-left: 32%;margin-top: 10px;">
				<view v-if="begin === false" class="time">
					<text class="time__item">距离开始</text>
					<view class="time__item">{{countdownh}}&nbsp;时</view>
					<view class="time__item">{{countdownm}}&nbsp;分</view>
					<view class="time__item">{{countdowns}}&nbsp;秒</view>
				</view>
				<view v-else="begin === true" class="time">
					<text class="time__item">距离结束</text>
					<view class="time__item">{{countdownh}}&nbsp;时</view>
					<view class="time__item">{{countdownm}}&nbsp;分</view>
					<view class="time__item">{{countdowns}}&nbsp;秒</view>
				</view>
			</view>
		</view>
		<view v-if="killGoodsList.length>0" class="goods-list">
			<view class="list" v-for="(item,index) in killGoodsList" :key="index">
				<view class="thumb">
					<image :src="ip+item.img"></image>
				</view>
				<view class="item">
					<view class="title">
						<text class="one-omit">{{item.goodsName}}</text>
					</view>
					<view class="price-data">
						<view class="price">
							<text>￥{{item.seckillPrice}}</text>
							<text class="lineation ">￥{{item.goodsPrice}}</text>
						</view>
						<view class="data">
							<view v-if="begin === true" class="btn" @click="onGoods(item)">
								<text>立即抢购</text>
							</view>
							<view v-else="begin === false" class="btn" style="background: #808080;">
								<text>立即抢购</text>
							</view>
							<view class="schedule">
								<view class="num">{{item.seckillNum}}</view>
								<view class="bar">
									<text></text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view v-else="killGoodsList.length<0" class="goods-list">
			<text style="font-size: 35rpx;margin-left: 30%;">暂无秒杀商品上架！</text>
		</view>
		<TabBar :tabBarShow="2"></TabBar>
	</view>
</template>

<script>
	import TabBar from '../../components/TabBar/TabBar.vue';
	import {baseUrl} from '@/config/baseUrl.js';
	export default {
		components: {
			TabBar,
		},
		data() {
			return {
				scrollTop: 0,
				killGoodsList: [],
				time: "",
				playTime:"",
				isEndTime: "01:00:00", //倒计时
				countdownh: '',
				countdownm: '',
				countdowns: '',
				timer: null, //重复执行
				begin: flase, //按钮判断
				ip:"http://43.142.173.112:9090/images/",
			};
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		onLoad() {
			this.getData();
			if(this.begin){
				this.truetime()
			}else{
				this.falsetime()
			}
		},
		methods: {
			truetime(){
				this.timer = setInterval(() => {
					var nowtime = new Date(), //获取当前时间
						endtime = new Date(this.playTime); //定义结束时间
					var lefttime = endtime.getTime() - nowtime.getTime(), //距离结束时间的毫秒数
						leftd = Math.floor(lefttime / (1000 * 60 * 60 * 24)), //计算天数
						lefth = Math.floor((lefttime / (1000 * 60 * 60) % 24) + leftd * 24) < 10 ? "0" + Math.floor((lefttime /
							(1000 * 60 * 60) % 24) + leftd * 24) : Math.floor((lefttime / (1000 * 60 * 60) % 24) + leftd *
							24), //计算小时数
						leftm = Math.floor(lefttime / (1000 * 60) % 60) < 10 ? "0" + Math.floor(lefttime / (1000 * 60) % 60) :
						Math.floor(lefttime / (1000 * 60) % 60), //计算分钟数
						lefts = Math.floor(lefttime / 1000 % 60) < 10 ? "0" + Math.floor(lefttime / 1000 % 60) : Math.floor(
							lefttime / 1000 % 60); //计算秒数
					this.countdownh = lefth //返回倒计时的字符串
					this.countdownm = leftm //返回倒计时的字符串
					this.countdowns = lefts //返回倒计时的字符串
					// 倒计时结束时，显示00:00:00
					if (lefttime < 0) {
						this.countdownh = this.countdownm = this.countdowns = "00";
						this.begin = false;
						this.falsetime();
					}
				},1000)	
			},
			falsetime() {
				this.timer = setInterval(() => {
					var nowtime = new Date(), //获取当前时间
						endtime = new Date(this.time); //定义结束时间
					var lefttime = endtime.getTime() - nowtime.getTime(), //距离结束时间的毫秒数
						leftd = Math.floor(lefttime / (1000 * 60 * 60 * 24)), //计算天数
						lefth = Math.floor((lefttime / (1000 * 60 * 60) % 24) + leftd * 24) < 10 ? "0" + Math.floor((lefttime /
							(1000 * 60 * 60) % 24) + leftd * 24) : Math.floor((lefttime / (1000 * 60 * 60) % 24) + leftd *
							24), //计算小时数
						leftm = Math.floor(lefttime / (1000 * 60) % 60) < 10 ? "0" + Math.floor(lefttime / (1000 * 60) % 60) :
						Math.floor(lefttime / (1000 * 60) % 60), //计算分钟数
						lefts = Math.floor(lefttime / 1000 % 60) < 10 ? "0" + Math.floor(lefttime / 1000 % 60) : Math.floor(
							lefttime / 1000 % 60); //计算秒数
					this.countdownh = lefth //返回倒计时的字符串
					this.countdownm = leftm //返回倒计时的字符串
					this.countdowns = lefts //返回倒计时的字符串
					// 倒计时结束时，显示00:00:00
					if (lefttime < 0) {
						this.countdownh = this.countdownm = this.countdowns = "00";
						this.begin = true;
						this.truetime();
					}
				},1000)	
			},
			/**
			 * 获取数据
			 */
			getData() {
				let shopsId = uni.getStorageSync("shopsId")
				uni.request({
					url: baseUrl + "/index/killGoods",
					method: 'POST',
					data: {
						shopsId: shopsId,
					},
					header: {
						"Content-Type": "application/x-www-form-urlencoded"
					},
					success: (res) => {
						//秒杀商品填充
						this.killGoodsList = res.data.killGoodsList;
						//倒计时
						var dt = res.data.startTime.replace(/-/g, "/");
						this.time = dt + " 16:00:00";
						this.playTime = dt + " 17:00:00";
					}
				})
			},

			/**
			 * 返回
			 */
			onBack() {
				uni.switchTab({
					url: '/pages/home/home'
				})
			},
			/**
			 * 商品点击
			 */
			onGoods(item) {
				uni.request({
					url: baseUrl + "/kiilGoods/buy",
					method: 'POST',
					data: {
						goodsId: item.goodsId,
						userId: uni.getStorageSync("userId")
					},
					header: {
						"Content-Type": "application/x-www-form-urlencoded"
					},
					success: (res) => {
						if(res.data == 1){
							uni.setStorageSync("kill",true);
							// uni.setStorageSync("goodsList",item)
							var list = JSON.stringify(item)
							uni.redirectTo({
								url: '/pages/ConfirmOrder/ConfirmOrder?goodsList=' + encodeURIComponent(list),
							})
						}else if(res.data == 2){
							uni.showToast({
								title: '该用户已经抢购过',
								icon: 'none',
							});
						}else if(res.data == 0){
							uni.showToast({
								title: '已抢空',
								icon: 'none',
							});
						}else{
							uni.showToast({
								title: '抢购异常',
								icon: 'none',
							});
						}
					}
				})
				
			}
		},
	}
</script>

<style scoped lang="scss">
	@import 'FlashSale.scss';

	.time {
		@include flex;
		align-items: center;

		&__item {
			color: #fff;
			font-size: 12px;
			text-align: center;
		}
	}
</style>
